﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using Hematology.Data;
using Hematology.HmtMembership;
using Hematology.Tools;
using Hematology.Tools.Storage;
using NgoResource = Hematology.UserSite.App_GlobalResources.UserSite;

namespace Hematology.UserSite.master
{
	public partial class Site : System.Web.UI.MasterPage
	{
		private bool _isLoginProcessStarted;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				if (MembershipHelper.MembershipService.IsAuthorizedRequest)
				{
					btnLogout.Text = String.Format("[ {0} ]", NgoResource.resExit);
					hlViewProfile.Text = String.Format("{0} {1}",
														!String.IsNullOrEmpty(SessionHelper.ActiveUser.FirstName)
															? SessionHelper.ActiveUser.FirstName
															: Page.User.Identity.Name,
														!String.IsNullOrEmpty(SessionHelper.ActiveUser.LastName)
															? SessionHelper.ActiveUser.LastName
															: string.Empty);

					bool bIsAdmin = MembershipHelper.RoleService.IsUserInRole(SessionHelper.ActiveUser.BaseInfo.Email,
																 EnumHelper.RequiredRoles.Admin.ToString());

					hlAdmin.Visible = bIsAdmin;
					lblSeparator1.Visible = bIsAdmin;

					mvLoginLogout.ActiveViewIndex = 1;
				}
				else if (CookieHelper.CookieUser != null)
				{
					SessionHelper.ActiveUser = CookieHelper.CookieUser;
					MembershipHelper.FormsService.SignIn(SessionHelper.ActiveUser.BaseInfo.Email, false);

					Response.Redirect(Request.Url.AbsoluteUri);
				}
				else
				{
					mvLoginLogout.ActiveViewIndex = 0;
				}
			}
		}

		protected void btnLogout_Click(object sender, EventArgs e)
		{
			MembershipHelper.FormsService.SignOut();
		}

		protected void btnLogin_Click(object sender, EventArgs e)
		{
			_isLoginProcessStarted = true;

			if (!MembershipHelper.RoleService.AdminExists())
			{
				Response.Redirect("/AccountProcessing.aspx?view=Registration&role=admin");
			}
			else
			{
				if (Page.IsValid)
				{
					string sEmail = txtEmail.Text.Trim();
					string sPassword = txtPassword.Text.Trim();

					if (!MembershipHelper.MembershipService.ValidateUser(sEmail, sPassword))
						return;

					SiteUser dbuser = SiteUser.SearchByEmail(sEmail);
					if (dbuser != null && dbuser.UserId != null)
					{
						SiteUserExtended siteUser = SiteUserExtended.Get(dbuser.UserId.Value);
						if (siteUser != null)
						{
							SessionHelper.ActiveUser = siteUser;
							MembershipHelper.FormsService.SignIn(sEmail, false);

							if (chkRememberMe.Checked)
								CookieHelper.CookieUser = siteUser;
						}
					}

					Response.Redirect(Request.Url.AbsoluteUri);
				}
			}
		}

		protected void btnSearch_Click(object sender, EventArgs e)
		{
			string sSearchText = txtSearchText.Text;
			if (String.IsNullOrEmpty(sSearchText.Trim()))
				return;
			
			Response.Redirect(String.Format("/Search.aspx?key={0}", HttpUtility.UrlEncode(txtSearchText.Text)));
		}

		protected void vldUserValidated_ServerValidate(object sender, ServerValidateEventArgs e)
		{
			e.IsValid = MembershipHelper.MembershipService.ValidateUser(txtEmail.Text, txtPassword.Text);
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (_isLoginProcessStarted)
			{
				ValidationHelper.RegisterServerValidationMessageScript(Page, "vgLogin");
			}
		}
	}
}